package com.zhihuichongcheng.application.ui.im.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.exceptions.HyphenateException;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.base.BaseActivity;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.ui.im.adapter.GroupMemberAdapter;

import java.util.List;

/**
 * Created by 3water on 2016/10/29.
 */

public class ImGroupDetailActivity extends BaseActivity implements View.OnClickListener{
    private TextView mTvGroupName;
    private String groupId;
    private TextView mTvExit;
    private GridView mRcvGroupMember;
    private GroupMemberAdapter mAdapter;
    private EMGroup group;
    private RelativeLayout mRlGroupName;
    private Handler mHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_im_group_detail);
        initView();
        initData();
    }

    private void initView() {
        setTitle("群聊资料");
        mRlGroupName = (RelativeLayout)findViewById(R.id.rl_group_name);
        mTvGroupName = (TextView)findViewById(R.id.tv_group_name);
        mTvExit = (TextView)findViewById(R.id.tv_exit);
        mRcvGroupMember = (GridView)findViewById(R.id.rcv_group_member);
        mRcvGroupMember.setNumColumns(4);
        mAdapter = new GroupMemberAdapter(mContext);
        mRcvGroupMember.setAdapter(mAdapter);
        mTvExit.setOnClickListener(this);
        mRlGroupName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mUser.getEasemobNumber().equals(group.getOwner()))
                {
                    Bundle bundle = new Bundle();
                    bundle.putString("title",group.getGroupName());
                    bundle.putString("id",groupId);
                    startActivityForResult(ImChatGroupNameActivity.class,0,bundle);
                }
            }
        });
    }

    private void initData() {

        Intent intent = getIntent();
        if(intent!=null)
        {
            groupId = intent.getStringExtra("userId");
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                   group = EMClient.getInstance().groupManager().getGroupFromServer(groupId);

                    final List<String> list = group.getMembers();
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            for(int i=0;i<list.size();i++)
                            {
                                UserBean userBean = spUtil.getObject(list.get(i),UserBean.class);
                                if(userBean==null)
                                {
                                    list.remove(i);
                                    i--;
                                }
                            }
                            mTvGroupName.setText(group.getGroupName());
                            if(mUser.getEasemobNumber().equals(group.getOwner()))
                            {
                                list.add("");
                            }
                            /***
                             * 检查一下是否都有对象
                             */



                            mAdapter.setGroupId(groupId);
                            mAdapter.setData(list);
                            mAdapter.notifyDataSetChanged();
                            if(mUser.getEasemobNumber().equals(group.getOwner()))
                            {
                                mTvExit.setText("解散群聊");
                            }
                            else
                            {
                                mTvExit.setText("退出群聊");
                            }
                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tv_exit:
                if(mUser.getEasemobNumber().equals(group.getOwner()))
                {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            EMClient.getInstance().groupManager().destroyGroup(groupId);//需异步处理
                            setResult(Activity.RESULT_OK);
                            finish();
                        } catch (HyphenateException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                }
                else
                {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                EMClient.getInstance().groupManager().leaveGroup(groupId);//需异步处理
                                setResult(Activity.RESULT_OK);
                                finish();
                            } catch (HyphenateException e) {
                                e.printStackTrace();
                            }
                        }
                    }).start();
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK)
        {
            if(data!=null)
            {
                if(data.hasExtra("title"))
                {
                    mTvGroupName.setText(data.getStringExtra("title"));
                }
            }
        }
    }
}
